// ActionScript file
import COMPONENTS.twChangeDineInStatus;
import COMPONENTS.twChangeTakeAwayStatus;
import COMPONENTS.twDeliveryItemsPrice;
import COMPONENTS.twHelp;
import COMPONENTS.twItemsPrice;

import ENUMS.ApplicationVariables;
import ENUMS.SlotStatus;

import EVENTS.SlotStatusChangedEvent;
import EVENTS.SwitchTableEvent;
import EVENTS.TableSelectedEvent;
import EVENTS.WaiterSelectedEvent;

import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.utils.Dictionary;

import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.managers.PopUpManager;
import mx.utils.StringUtil;

import org.flexlancer.Logger.Logger;
import org.flexlancer.databases.AppUsers;
import org.flexlancer.enums.BillType;
import org.flexlancer.utils.CacheUtils;

[Bindable]
private var xmllistCategories:XMLList = new XMLList();
[Bindable]
private var xmllistItems:XMLList = new XMLList();

private var dineInData:Dictionary = new Dictionary();
private var takeawayData:Dictionary = new Dictionary();
private var deliveryData:Dictionary = new Dictionary();

private function OnCC():void{
	try{
		Logger.log_file_name = "BENAROUS.log";
		Logger.Log("===================================================================");
		Logger.Log("BENROUS application loads");
		Logger.Log("===================================================================");
		
		this.maximize();
		this.addEventListener(KeyboardEvent.KEY_UP,OnAppKeyUp);
		Logger.Log("Reading Cache");
		
		CacheUtils.ReadCache(this.dineInData,ApplicationVariables.DINE_IN_CACHE);
		CacheUtils.ReadCache(this.takeawayData,ApplicationVariables.TAKE_AWAY_CACHE);
		CacheUtils.ReadCache(this.deliveryData,ApplicationVariables.DELIVERY_CACHE);
		
		this.txtUserID.setFocus();
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private var twH:twHelp = null;
private function OnAppKeyUp(evt:KeyboardEvent):void{
	try{
		switch(evt.keyCode){
			case Keyboard.F1:
					this.twH = PopUpManager.createPopUp(this,twHelp,true) as twHelp;
					PopUpManager.centerPopUp(this.twH);
			break;
			case Keyboard.F9:
				this.currentState = "Delivery";
			break;
			case Keyboard.F11:
				this.currentState = "Take away";
			break;
			case Keyboard.F12:
				this.currentState = "Dine In";
			break;
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
	
}
//-----------------------------------------------------------------------
//                 Take Away Starts
//-----------------------------------------------------------------------
private var selectedTakeAway:XML;

private function OnEnterTakeAway():void{
	try{
		Logger.Log("Setting TakeAway Cache Data");
		var key:String;
		var index:int;
		var tdata:XML;
		
		this.shTakeAway.addEventListener(TableSelectedEvent.TABLE_SELECTED,OnTakeAway_TableSelected);	

		for(key in this.takeawayData ) {
			index = int(key) - 1;
			tdata = this.shTakeAway.getItem(index);
			
			tdata.@waitername = this.takeawayData[key].server;
			tdata.@status = SlotStatus.FILL;
		} 
		this.shTakeAway.setFocus();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnExitTakeAway():void{
	try{
		this.shTakeAway.removeEventListener(TableSelectedEvent.TABLE_SELECTED,OnTakeAway_TableSelected);
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnTakeAway_TableSelected(evt:TableSelectedEvent):void{
	try{
		this.selectedTakeAway = evt.Data;
		if ( this.selectedTakeAway.@status == SlotStatus.FILL ){
			var key:String = String(this.selectedTakeAway.@tablename);
			this.ShowTakeAwayItems(this.takeawayData[key]);
		}else{
			var twWaiter:twChangeTakeAwayStatus = PopUpManager.createPopUp(this,twChangeTakeAwayStatus,true) as twChangeTakeAwayStatus;
			PopUpManager.centerPopUp(twWaiter);
			twWaiter.addEventListener(WaiterSelectedEvent.WAITER_SELECTED,OnTakeAwayWaiterSelected);
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnTakeAwayWaiterSelected(evt:WaiterSelectedEvent):void{
	try{
		this.selectedTakeAway.@waitername = evt.WaiterName;
		this.selectedTakeAway.@status = SlotStatus.FILL;
		this.ShowTakeAwayItems();	
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function ShowTakeAwayItems(parmData:Object = null):void {
	try {
		var twItemsandPrice:twItemsPrice = PopUpManager.createPopUp(this,twItemsPrice,true) as twItemsPrice;
		twItemsandPrice.addEventListener(SlotStatusChangedEvent.SLOT_STATUS_CHANGED,OnTakeAwaySlotStatusChanged);
		twItemsandPrice.addEventListener(SwitchTableEvent.ASK_FOR_AVAILABLE_SLOTS,OnTakeAwayAskForTables);
		twItemsandPrice.QTData = parmData ;//!= null ? parmData.QTData : null ;
		twItemsandPrice.title = "Serving \"" + String(this.selectedTakeAway.@tablename) + "\" by \"" + String(this.selectedTakeAway.@waitername)+"\"";
		twItemsandPrice.slotid = Number(this.selectedTakeAway.@tablename);
		twItemsandPrice.server =String(this.selectedTakeAway.@waitername);
		twItemsandPrice.cat = BillType.TAKE_AWAY; 
		twItemsandPrice.MenuFile = ApplicationVariables.TAKEY_AWAY_MENU;
		PopUpManager.centerPopUp(twItemsandPrice);
	}catch(err:Error) {
		Logger.LogE(err);
	}
}

private function OnTakeAwayAskForTables(evt:SwitchTableEvent):void{
	try{
		var arrOptions:Array = new Array;
		for each ( var eachTable:XML in this.dihHall.testData ){
			if ( eachTable.@status == SlotStatus.FREE ){
				var name:String = String(eachTable.@tablename);
				arrOptions.push(eachTable);
			}
		}
		twItemsPrice(evt.target).SetAvailableTables(arrOptions);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnTakeAwaySlotStatusChanged(evt:SlotStatusChangedEvent):void{
	try{
		var key:String = String(this.selectedTakeAway.@tablename); 
		switch(evt.SlotStatus){
			case SlotStatus.PENDING:
			case SlotStatus.REPORTING:
			case SlotStatus.FILL:
				this.takeawayData[key] = evt.SlotData;
			break;
			case SlotStatus.FREE:
				this.selectedTakeAway.@waitername = "";
				this.selectedTakeAway.@status = SlotStatus.FREE;
				delete this.takeawayData[key];
			break;
		}
		CacheUtils.WriteCache(this.takeawayData,ApplicationVariables.TAKE_AWAY_CACHE);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

//-----------------------------------------------------------------------
//                 Take Away Ends
//-----------------------------------------------------------------------

//-----------------------------------------------------------------------
//                 Delivery Starts
//-----------------------------------------------------------------------
private var selectedDelivery:XML;

private function OnEnterDelivery():void{
	try{
		Logger.Log("Setting Delivery Cache Data");
		var key:String;
		var index:int;
		var tdata:XML;

		this.shDelivery.addEventListener(TableSelectedEvent.TABLE_SELECTED,OnDelivery_TableSelected);
		
		for(key in this.deliveryData ) {
			index = int(key) - 1;
			tdata = this.shDelivery.getItem(index);
			
			tdata.@waitername = this.deliveryData[key].server;
			tdata.@status = SlotStatus.FILL;
		} 			
		this.shDelivery.setFocus();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnExitDelivery():void{
	try{
		this.shDelivery.removeEventListener(TableSelectedEvent.TABLE_SELECTED,OnDelivery_TableSelected);
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnDelivery_TableSelected(evt:TableSelectedEvent):void{
	try{
		this.selectedDelivery = evt.Data;
		if ( this.selectedDelivery.@status == SlotStatus.FILL ){
			var key:String = String(this.selectedDelivery.@tablename);
			this.ShowDeliveryItems(this.deliveryData[key]);
		}else{
			/*
			var twWaiter:twChangeDeliveryStatus = PopUpManager.createPopUp(this,twChangeDeliveryStatus,true) as twChangeDeliveryStatus;
			PopUpManager.centerPopUp(twWaiter);
			twWaiter.addEventListener(WaiterSelectedEvent.WAITER_SELECTED,OnDeliveryRiderSelected);
			*/
			this.selectedDelivery.@waitername = "NO RIDER";
			this.selectedDelivery.@status = SlotStatus.FILL;
			this.ShowDeliveryItems();	
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnDeliveryRiderSelected(evt:WaiterSelectedEvent):void{
	try{
		this.selectedDelivery.@waitername = evt.WaiterName;
		this.selectedDelivery.@status = SlotStatus.FILL;
		this.ShowDeliveryItems();	
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function ShowDeliveryItems(parmData:Object = null):void {
	try {
		var twItemsandPrice:twDeliveryItemsPrice = PopUpManager.createPopUp(this,twDeliveryItemsPrice,true) as twDeliveryItemsPrice;
		twItemsandPrice.addEventListener(SlotStatusChangedEvent.SLOT_STATUS_CHANGED,OnDeliverySlotStatusChanged);
		twItemsandPrice.sel_item_of_slot = this.selectedDelivery;
		twItemsandPrice.QTData = parmData;
		twItemsandPrice.title = "Serving \"" + String(this.selectedDelivery.@tablename) + "\" by \"" + String(this.selectedDelivery.@waitername)+"\"";
		twItemsandPrice.slotid = Number(this.selectedDelivery.@tablename);
		twItemsandPrice.server =String(this.selectedDelivery.@waitername);
		twItemsandPrice.cat = BillType.DELIVERY; 
		twItemsandPrice.MenuFile = ApplicationVariables.DELIVERY_MENU;
		PopUpManager.centerPopUp(twItemsandPrice);
	}catch(err:Error) {
		Logger.LogE(err);
	}
}

private function OnDeliverySlotStatusChanged(evt:SlotStatusChangedEvent):void{
	try{
		var key:String = String(this.selectedDelivery.@tablename); 
		switch(evt.SlotStatus){
			case SlotStatus.PENDING:
			case SlotStatus.REPORTING:
			case SlotStatus.FILL:
				this.deliveryData[key] = evt.SlotData;
			break;
			case SlotStatus.FREE:
				this.selectedDelivery.@waitername = "";
				this.selectedDelivery.@status = SlotStatus.FREE;
				delete this.deliveryData[key];
			break;
		}
		CacheUtils.WriteCache(this.deliveryData,ApplicationVariables.DELIVERY_CACHE);		
	}catch(err:Error){
		Logger.LogE(err);
	}
}

//-----------------------------------------------------------------------
//                 Delivery Ends
//-----------------------------------------------------------------------

//-----------------------------------------------------------------------
//                 Dine In Starts
//-----------------------------------------------------------------------

private var selectedTable:XML;

private function OnEnterDineIn():void{
	try{
		Logger.Log("Setting Dine In Cache Data");
		var key:String;
		var index:int;
		var tdata:XML;
		this.dihHall.addEventListener(TableSelectedEvent.TABLE_SELECTED,OnDinIn_TableSelected);		
		
		for(key in this.dineInData ) {
			index = int(key) - 1;
			tdata = this.dihHall.getItem(index);
			
			tdata.@waitername = this.dineInData[key].server;
			tdata.@status = SlotStatus.FILL;
		}
		this.dihHall.setFocus();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnExitDineIn():void{
	try{
		this.dihHall.removeEventListener(TableSelectedEvent.TABLE_SELECTED,OnDinIn_TableSelected);		
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnDinIn_TableSelected(evt:TableSelectedEvent):void{
	try{
		this.selectedTable = evt.Data;
		if ( this.selectedTable.@status == SlotStatus.FILL ){
			var key:String = String(this.selectedTable.@tablename);
			this.ShowItemsandPrice(this.dineInData[key]);
		}else{
			var twWaiter:twChangeDineInStatus = PopUpManager.createPopUp(this,twChangeDineInStatus,true) as twChangeDineInStatus;
			PopUpManager.centerPopUp(twWaiter);
			twWaiter.addEventListener(WaiterSelectedEvent.WAITER_SELECTED,OnWaiterSelected);
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnWaiterSelected(evt:WaiterSelectedEvent):void{
	try{
		this.selectedTable.@waitername = evt.WaiterName;
		this.selectedTable.@status = SlotStatus.FILL;
		this.ShowItemsandPrice();	
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function ShowItemsandPrice(parmData:Object = null):void {
	try {
		var twItemsandPrice:twItemsPrice = PopUpManager.createPopUp(this,twItemsPrice,true) as twItemsPrice;
		twItemsandPrice.addEventListener(SlotStatusChangedEvent.SLOT_STATUS_CHANGED,OnDinInSlotStatusChanged);
		twItemsandPrice.addEventListener(SwitchTableEvent.ASK_FOR_AVAILABLE_SLOTS,OnDineInAskForTables);
		twItemsandPrice.QTData = parmData;//parmData != null ? parmData.QTData : null ;
		twItemsandPrice.title = "Serving \"" + String(this.selectedTable.@tablename) + "\" by \"" + String(this.selectedTable.@waitername)+"\"";
		twItemsandPrice.slotid = Number(this.selectedTable.@tablename);
		twItemsandPrice.server =String(this.selectedTable.@waitername);
		twItemsandPrice.cat = BillType.DINE_IN; 
		twItemsandPrice.MenuFile = ApplicationVariables.DINE_IN_MENU;
		twItemsandPrice.defaultServiceCharges = true;
		PopUpManager.centerPopUp(twItemsandPrice);
	}catch(err:Error) {
		Logger.LogE(err);
	}
}

private function OnDineInAskForTables(evt:SwitchTableEvent):void{
	try{
		var arrOptions:Array = new Array;
		for each ( var eachTable:XML in this.dihHall.testData ){
			if ( eachTable.@status == SlotStatus.FREE ){
				var name:String = String(eachTable.@tablename);
				if ( name != String(this.selectedTable.@tablename) )
					arrOptions.push(eachTable);
			}
		}
		twItemsPrice(evt.target).SetAvailableTables(arrOptions);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnDinInSlotStatusChanged(evt:SlotStatusChangedEvent):void{
	try{
		var key:String = String(this.selectedTable.@tablename); 
		switch(evt.SlotStatus){
			case SlotStatus.PENDING:
			case SlotStatus.REPORTING:
			case SlotStatus.FILL:
				this.dineInData[key] = evt.SlotData;
			break;
			case SlotStatus.TABLE_SWITCH:
				var waitername:String = String(this.selectedTable.@waitername);
				
				this.selectedTable.@waitername = "";
				this.selectedTable.@status = SlotStatus.FREE;
				delete this.dineInData[key];
				
				key = evt.SlotData.newTable;
				delete evt.SlotData.newTable;
				
				this.selectedTable = this.dihHall.testData.(@tablename == key)[0];
				this.selectedTable.@waitername = waitername;
				this.selectedTable.@status = SlotStatus.FILL;
				this.dineInData[key] = evt.SlotData;
				
			break;
			case SlotStatus.FREE:
				this.selectedTable.@waitername = "";
				this.selectedTable.@status = SlotStatus.FREE;
				delete this.dineInData[key];
			break;
		}
		CacheUtils.WriteCache(this.dineInData,ApplicationVariables.DINE_IN_CACHE);		
	}catch(err:Error){
		Logger.LogE(err);
	}
}

//-----------------------------------------------------------------------
//                 Dine In Ends
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//                 Login Starts
//-----------------------------------------------------------------------
[Bindable]
private var welcome_lbl:String = "";

private function OnLogin():void{
	try{
		this.txtUserID.text = StringUtil.trim(this.txtUserID.text);
		this.txtPassword.text = StringUtil.trim(this.txtPassword.text);
		
		var arr:Array = AppUsers.List("userid LIKE '"+this.txtUserID.text+"' AND password LIKE '"+this.txtPassword.text+"'");
		
		if ( arr.length == 0 ){
			Alert.show("Invalid UserID / Password Combination","Error in Login",4,this,this.OnLoginError);
		} else if ( arr[0].allowpos == 0 ){
			Alert.show("Your UserID is blocked. Please contact to administrator","Error in Login",4,this,this.OnLoginError);
		} else {
			ApplicationVariables.LOGIN_USER = arr[0].userid;
			ApplicationVariables.LOGIN_PASSWORD = arr[0].password;
			this.welcome_lbl = "WELCOME MR."+arr[0].userid.toUpperCase();
			this.currentState = "Dine In";
		}
		
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnLoginError(evt:CloseEvent):void{
	try{
		this.txtUserID.setFocus();
	}catch(err:Error){
		Logger.LogE(err);
	}
}
//-----------------------------------------------------------------------
//                 Login Ends
//-----------------------------------------------------------------------
